package com.easy.yours.biz.controller;

import com.easy.system.controller.BaseController;
import com.easy.system.util.*;
import com.easy.yours.biz.model.Demo;
import com.easy.yours.biz.service.DemoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-7-1
 * Time: 上午9:03
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class DemoController extends BaseController<Demo> {
    @Resource
    private DemoService demoService;

    public DemoController() {
        super("demo", Demo.class);
        super.setBizService(demoService);
    }

    @ResponseBody
    @RequestMapping
    protected void getGridData(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<Demo> entitys = null;
        Long total = null;
        Pagination pagination = RequestVarsUtil.getPagination(request);
        String type = request.getParameter("searchType");

        if (type != null) {
            if (type.equals(Constant.ADVANCE_SEARCH)) {
//                String[] allParams = new String[]{"property1", "property2" };
                String[] allParams = null;
                entitys = super.getAdvanceQueryEntitys(request, pagination, allParams);
                total = super.getAdvanceQueryTotal(request, allParams);
            } else {
                String property = request.getParameter("searchProperty");
                String searchValue = request.getParameter("searchValue");
//                Object[] allProperties = new Object[]{"property1", "property2" };
                Object[] allProperties = null;
                entitys = super.getSimpleQueryEntitys(property, searchValue, allProperties, pagination);
                total = super.getSimpleQueryTotal(property, searchValue, allProperties);
            }
        } else {
            entitys = super.getEntitys(pagination);
            total = super.getTotal();
        }
        ResultDto<List<Demo>> resultDto = new ResultDto<List<Demo>>();
        resultDto.setRows(entitys);
        resultDto.setTotal(total);
        resultDto.setSuccess(true);
        writeToClient(response, resultDto.toString());
    }

    //跳到主页
    @RequestMapping
    public String index(Model model, HttpServletRequest request) {
        super.setBizService(demoService);
        super.setModel(model);
        return super.toView(request);
    }

    //编辑界面
    @RequestMapping
    public String edit(Model model, HttpServletRequest request, Long id) {
        super.setBizService(demoService);
        Demo demo = super.getEntity(id);
        super.setModel(model, demo);
        return super.toView(request);
    }

    //添加界面
    @RequestMapping
    public String add(Model model, HttpServletRequest request) {
        super.setBizService(demoService);
        super.setModel(model);
        return super.toView(request);
    }

    //查看界面
    @RequestMapping
    public String view(Model model, HttpServletRequest request, Long id) {
        super.setBizService(demoService);
        Demo demo = super.getEntity(id);
        super.setModel(model, demo);
        return super.toView(request);
    }

    //保存操作
    @RequestMapping
    public void saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        super.setBizService(demoService);
        MessageDto messageDto = new MessageDto();
        Demo demo = super.injectObject(request, Demo.class);
        try {
            if (demo.getId() != null) {
                super.updateEntity(demo);
                super.setMessageDto(messageDto, MessageDefined.update_success_info);
            } else {
                this.demoService.save(demo);
                super.setMessageDto(messageDto, MessageDefined.save_success_info);
            }
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
        }
        super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void delete(Long id, HttpServletResponse response) throws IOException {
        super.setBizService(demoService);
        MessageDto messageDto = new MessageDto();
        try {
            super.deleteEntity(id);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
        }
        super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void deleteMore(HttpServletRequest request, HttpServletResponse response) throws IOException {
        super.setBizService(demoService);
        String ids = request.getParameter("ids");
        MessageDto messageDto = new MessageDto();
        try {
            Integer count = super.deleteEntitys(ids);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
        }
        super.writeToClient(response, messageDto.toString());
    }


}
